package com.itcast.mallapplet.controller.user;

import com.itcast.applet.dto.DityPageRequest;
import com.itcast.applet.entity.Commodity;
import com.itcast.mallapplet.service.CommodityService;
import com.itcast.result.PageResult;
import com.itcast.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/dity")
@Slf4j
@Api(tags = "用户端-商品管理")
public class CommodityController {
    @Autowired
    private CommodityService commodityService;

    @PostMapping("/querylist")
    @ApiOperation("根据分类id查询,分页,加模糊查询")
    @Cacheable(cacheNames = "setmealCache", key = "#dityPageRequest")
    public Result<PageResult> dityQuerylist(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                            @RequestBody DityPageRequest dityPageRequest) {
        log.info("分页查询信息为：{}", dityPageRequest);
        dityPageRequest.setPage(page);
        dityPageRequest.setPageSize(pageSize);
        PageResult dityVOlist = commodityService.dityQuerylist(dityPageRequest);
        return Result.success(dityVOlist);
    }

    @GetMapping("/query")
    @ApiOperation("根据id查询单个商品")
    @Cacheable(cacheNames = "setmealCache", key = "#id")
    public Result<Commodity> dityQuery(Long id) {
        log.info("查询单个商品的id为：{}", id);
        Commodity dity = commodityService.dityQuery(id);
        return Result.success(dity);
    }



}
